package com.papper.common.utils.tool;


import tk.mybatis.mapper.util.StringUtil;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @Author:Lajiao
 * @Date:2016/3/7
 * @Description:
 */
public class OrderTool {
    private static final AtomicLong atomicLong      = new AtomicLong(0);
    private static final AtomicLong atomicSettle    = new AtomicLong(0);
    private static final AtomicLong atomicOrder     = new AtomicLong(0);
    private static final AtomicLong atomicApply     = new AtomicLong(0);
    private static final int        DEFAULT_RANDOM  = 6;
    public static final  String     ORDER_RECORD    = "1";
    public static final  String     ORDER_FINANCIAL = "7";
    public static final  String     ORDER_PROFITE   = "8";
    public static final  String     ORDER_SETTLE    = "9";

    /**
     * 生成顺序码
     *
     * @param digit
     * @return
     */
    public static String getSeqId(int digit) {
        long v = atomicLong.getAndIncrement();
        v = v + 1;
        String s      = String.valueOf(v);
        int    length = s.length();
        if (s.length() < digit) {
            for (int i = 0; i <= (digit - length) + 1; i++) {
                s = "0" + s;
            }
        }
        return s.substring(s.length() - digit);
    }

    public static String getSeqSettle(int digit) {
        long v = atomicSettle.getAndIncrement();
        v = v + 1;
        String s      = String.valueOf(v);
        int    length = s.length();
        if (s.length() < digit) {
            for (int i = 0; i <= (digit - length) + 1; i++) {
                s = "0" + s;
            }
        }
        return s.substring(s.length() - digit);
    }

    public static String getSeqOrder(int digit) {
        long v = atomicOrder.getAndIncrement();
        v = v + 1;
        String s      = String.valueOf(v);
        int    length = s.length();
        if (s.length() < digit) {
            for (int i = 0; i <= (digit - length) + 1; i++) {
                s = "0" + s;
            }
        }
        return s.substring(s.length() - digit);
    }

    public static String getSeqApply(int digit) {
        long v = atomicApply.getAndIncrement();
        v = v + 1;
        String s      = String.valueOf(v);
        int    length = s.length();
        if (s.length() < digit) {
            for (int i = 0; i <= (digit - length) + 1; i++) {
                s = "0" + s;
            }
        }
        return s.substring(s.length() - digit);
    }

    /**
     * 随即生成固定长度字符串
     *
     * @param length
     * @return
     */
    public static String getRandomString(int length) {
        String       base   = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random       random = new Random();
        StringBuffer sb     = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString().toUpperCase();
    }

    public static String getRandomPass(int length) {
        String       base   = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random       random = new Random();
        StringBuffer sb     = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static String getRandomString16(int length) {
        String       base   = "abcdef0123456789";
        Random       random = new Random();
        StringBuffer sb     = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 随即生成固定长度数字串
     *
     * @param length
     * @return
     */
    public static String getRandomInteger(int length) {
        String       base   = "0123456789";
        Random       random = new Random();
        StringBuffer sb     = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 位数补零
     *
     * @param data
     * @param length
     * @return
     */
    public static String zerofill(String data, Integer length) {
        String str = (data == null || data.isEmpty() ? "" : data);
        for (int i = 0; i < length; i++) {
            str = "0" + str;
        }
        return str.substring(str.length() - length);
    }

    /**
     * 生成16位动态唯一编号[G+{12位时间}+{2位随机号}+{3位随机号}]
     *
     * @return
     */
    public static String GuidTo16String() {
        SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
        return format.format(Calendar.getInstance().getTime()) + getRandomInteger(1).toUpperCase() + getSeqId(1);
    }

    public static String GuidTo16Integer() {
        SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
        return format.format(Calendar.getInstance().getTime()) + getRandomInteger(2).toUpperCase() + getSeqId(2);
    }

    public static String getUUID() {
        String s = UUID.randomUUID().toString();
        return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
    }

    public static String getApplyNo(String payType) {
        StringBuffer orderId = new StringBuffer();
        orderId.append(payType);
        orderId.append("1");
        orderId.append("1");
        orderId.append(DateTool.formatDate(new Date(), "yyMMddHHmmss")).append(getRandomInteger(2)).append(getSeqApply(3));
        return orderId.toString();
    }

    public static String getTaskCode(Integer taskType) {
        StringBuffer taskCode = new StringBuffer();
        taskCode.append(taskType);
        taskCode.append(DateTool.formatDate(new Date(), "yyMMddHHmmss")).append(getRandomInteger(1)).append(getSeqId(1)).append(getSeqOrder(3));
        return taskCode.toString();
    }

    public static String getOrderNo(Integer payType) {
        StringBuffer orderId = new StringBuffer();
        orderId.append(ORDER_RECORD);
        orderId.append("1");
        orderId.append("1");
        orderId.append(DateTool.formatDate(new Date(), "yyMMddHHmmss")).append(getRandomInteger(1)).append(getSeqId(1)).append(getSeqOrder(3));
        return orderId.toString();
    }

    public static String getSettleNo(Integer payType) {
        StringBuffer orderId = new StringBuffer();
        orderId.append(ORDER_SETTLE);
        orderId.append("1");
        orderId.append("1");
        orderId.append(DateTool.formatDate(new Date(), "yyMMddHHmmss")).append(getRandomInteger(1)).append(getSeqId(1)).append(getSeqSettle(3));
        return orderId.toString();
    }

    public static String getShopCode(String phone) {
        StringBuffer shopCode = new StringBuffer();
        if (StringUtil.isEmpty(phone)) {
            shopCode.append("9").append(getRandomInteger(4)).append(getRandomInteger(3));
        } else if (phone.length() <= 4) {
            shopCode.append("9").append(phone).append(getRandomInteger(4 - phone.length())).append(getRandomInteger(3));
        } else {
            shopCode.append("9").append(phone.substring(phone.length() - 4)).append(getRandomInteger(3));
        }

        return shopCode.toString();
    }

    public static String getOrgCode(String phone) {
        phone = StringTool.nullToEmpty(phone);
        StringBuffer shopCode = new StringBuffer();
        shopCode.append(DateTool.formatDate(new Date(), "yyMMdd")).append(phone.substring(phone.length() - 4)).append(getRandomInteger(6));
        return shopCode.toString();
    }

    public static String getMD5Num(String source) {
        String s         = null;
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(source.getBytes(StandardCharsets.UTF_8));
            byte[] tmp = md.digest();
            char[] str = new char[16];
            int    k   = 0;
            for (int i = 0; i < 16; i++) {
                byte byte0 = tmp[i];
                str[k++] = hexDigits[(byte0 >>> 4 & 0xf) % 10];
            }
            s = new String(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }

    public static void main(String[] args) {
        System.out.println(OrderTool.getSeqId(1));
        System.out.println(OrderTool.GuidTo16Integer());
    }
}
